<#include "./template/general_temp.ftl">

<#macro content>
<div class="row">
    <div class="box col-md-12">
        <div class="box-inner">
            <div class="box-header well" data-original-title="">
                <h2><i class="glyphicon glyphicon-dictCats"></i>用户管理</h2>

                <div class="box-icon">
                    <a href="#" class="btn btn-setting btn-round btn-default"><i class="glyphicon glyphicon-cog"></i></a>
                    <a href="#" class="btn btn-minimize btn-round btn-default"><i
                            class="glyphicon glyphicon-chevron-up"></i></a>
                    <a href="#" class="btn btn-close btn-round btn-default"><i class="glyphicon glyphicon-remove"></i></a>
                </div>
            </div>
            <div class="box-content">
                <div class="alert alert-info">
                    <a href="#" class="btn btn-info btn-setting add">新增</a>
                </div>
                <table id="dictCats_dg">
                </table>
            </div>
        </div>
    </div>
    <!--/span-->
    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         aria-hidden="true">

        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">×</button>
                    <h3>添加分类</h3>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="addCat">分类名称</label>
                        <input type="text" class="form-control" id="addCat"/>
                    </div>
                </div>
                <div class="modal-footer">
                    <a href="#" class="btn btn-primary saveAdd" data-dismiss="modal">保存</a>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         aria-hidden="true">

        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">×</button>
                    <h3>编辑分类</h3>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="editCat">分类名称</label>
                        <input type="text" class="form-control" id="editCat"/>
                        <input type="hidden" id="catId"/>
                    </div>
                </div>
                <div class="modal-footer">
                    <a href="#" class="btn btn-primary saveEdit" data-dismiss="modal">保存</a>
                </div>
            </div>
        </div>
    </div>
</div><!--/row-->
</#macro>

<#macro script>
<script type="text/javascript">
    $(document).ready(function () {
        $('.add').click(function (e) {
            e.preventDefault();
            $('#addModal').modal('show');
        });

        $('.saveAdd').click(function (e) {
            e.preventDefault();
            saveAdd();
        });

        $('.saveEdit').click(function (e) {
            e.preventDefault();
            saveEdit();
        });

        query();
    });

    function query(){
        //构造列表
        jQuery('#dictCats_dg').datagrid({
            url:'${base}/system/meta/list',
            fitColumns : true,
            striped : true,
            pagination : true,
            rownumbers : true,
            pageSize : 50,
            pageNumber : 1,
            pageList : [ 50, 100, 150, 200, 250],
            loadMsg : '数据正在加载中...',
            singleSelect:true,
            queryParams : {"type":5},
            selectOnCheck : false,
            checkOnSelect : false,
            width:$('.box-content').width() * 0.985,
            columns : [[
                {field : 'name', title : '分类名称',  resizable : true, hidden : false, sortable : false, width:$(this).width()*0.05, align: 'center'},
                {field : 'ctrl', title : '操作',  resizable : true, hidden : false, sortable : false, width:$(this).width()*0.05, align: 'center',
                    formatter:function(value,row,index){
                        var editBtn = "<a class='btn btn-info' href='javascript:void(0)' onclick='edit(" + row.id +")'><i class='glyphicon glyphicon-edit icon-white'></i>编辑</a>";
                        var delBtn = "<a class='btn btn-primary' href='javascript:void(0)' onclick='del(" + row.id +")'><i class='glyphicon glyphicon-trash icon-white'></i>删除</a>"

                        return editBtn + delBtn;
                    }
                }
            ]],
            onLoadSuccess:function(data){
            },
            onClickRow: function (rowIndex, rowData) {

            }
        });
    }

    function edit(id){
        jQuery.ajax({
            type: "GET",
            url: '${base}/system/dictCats/get',
            dataType:"json",
            data: {"id": id},
            success: function(data){
                if(data.status == 1) {
                    $("#editCat").val(data.data.name);
                    $("#catId").val(data.data.id);
                }
            },
            error: function (obj){
                console.info(obj);
            }
        });
        $('#editModal').modal('show');
    }

    function saveEdit(){
        var id= $("#catId").val();
        var name = $("#editCat").val();
        jQuery.ajax({
            type: "POST",
            url: '${base}/system/dictCats/edit',
            dataType:"json",
            data: {"id": id,"name":name},
            success: function(data){
                if(data.status == 1) {
                    alert("修改成功！");
                    $('#editModal').fadeOut();
                    $("#editCat").val("");
                    query();
                }else{
                    alert(data.msg);
                }
            },
            error: function (obj){
                console.info(obj);
            }
        });
    }

    function saveAdd(){
        var name = $("#addCat").val();
        jQuery.ajax({
            type: "POST",
            url: '${base}/system/dictCats/add',
            dataType:"json",
            data: {"name": name},
            success: function(data){
                if(data.status == 1) {
                    alert("添加成功！");
                    $("#addCat").val("");
                    $('#addModal').fadeOut();
                    query();
                }else{
                    alert(data.msg);
                }
            },
            error: function (obj){
                console.info(obj);
            }
        });
    }

    function del(id){
        if(window.confirm("确认删除？")) {
            jQuery.ajax({
                type: "POST",
                url: '${base}/system/meta/delete',
                dataType:"json",
                data: {"id": id},
                success: function(data){
                    if(data.status == 1) {
                        alert("删除成功！");
                        query();
                    }
                },
                error: function (obj){
                    console.info(obj);
                }
            });
        }
    }
</script>
</#macro>
